package com.zys.asseter.controller;

import com.zys.asseter.domain.dto.UserDTO;
import com.zys.asseter.domain.pojo.PageBean;
import com.zys.asseter.domain.pojo.Result;
import com.zys.asseter.domain.pojo.UserEntity;
import com.zys.asseter.domain.vo.TreeVo;
import com.zys.asseter.domain.vo.UserQueryVo;
import com.zys.asseter.mapper.UserMapper;
import com.zys.asseter.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    private final UserMapper userMapper;

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<UserEntity>> selectAll() {
        List<UserEntity> userList = userMapper.selectAll();
//        List<UserEntity> collect = userList.stream().filter(s -> s.getName().equals("张三")).collect(Collectors.toList());
        return Result.success(userList);
    }

    /**
     * 按照姓名，年龄构建树形结构
     *
     * @return
     */
    @GetMapping("/typeTree")
    public Result getTypeTree() {
        List<TreeVo> typeTree = userService.getTypeTree();
        return Result.success(typeTree);
    }

    /**
     * 条件分页查询用户信息a
     *
     * @param userQueryVo
     * @return
     */
    @GetMapping("/select")
    public Result selectCondition(UserQueryVo userQueryVo) {
        if (userQueryVo.getPageNum() == null && userQueryVo.getPageNum() < 1) {
            userQueryVo.setPageNum(1);
        }
        if (userQueryVo.getPageSize() == null && userQueryVo.getPageSize() < 1) {
            userQueryVo.setPageSize(10);
        }
        PageBean<UserEntity> pb = userService.selectCondition(userQueryVo);
        return Result.success(pb);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        UserEntity userEntity = userMapper.selectById(id);
        return Result.success(userEntity);
    }

    @PostMapping("/delete")
    public Result deleteById(List<Integer> ids) {
        userMapper.deleteById(ids);
        return Result.success();
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody UserDTO userDTO) {
        userService.insert(userDTO);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestParam("name") String name) {
        Integer i = userMapper.countByName(name);
        String count = String.valueOf(i);
        userService.update(name, count);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestParam("name") String name) {
        if (name != null && !"".equals(name)) {
            return Result.success();
        } else {
            return Result.error("0");
        }
    }

    @GetMapping("/count/name")
    public Result<List<UserDTO>> countByName() {
        List<UserDTO> userDTO = userMapper.selectNameAge();
        return Result.success(userDTO);
    }

    @GetMapping("/list")
    public Result selectByName(@RequestParam("name") String name) {
        String s = userMapper.getCharacterByName(name);
        String[] split = s.split(",");
        for (String string : split) {
            System.out.println(string);
        }
        return Result.success(split);
    }

}
